package com.minosiants.confluence.blog.plugin.impl.tweet2it;

/*
 * Copyright 2009 Kaspar Minosiants http://www.minosiants.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import oauth.signpost.OAuthConsumer;

import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import oauth.signpost.impl.DefaultOAuthConsumer;
import oauth.signpost.impl.DefaultOAuthProvider;
import oauth.signpost.signature.SignatureMethod;

/**
 *	Project home: http://tweet2it.minosiants.com
 *
 *  @author Kaspar Minosiants
 *
 * 	$Id:$
 */
public class TwitterOAuthController{
	private Log logger = LogFactory.getLog(getClass());
	private DefaultOAuthProvider provider;
	private OAuthConsumer consumer;	
	
	public TwitterOAuthController() {
		super();
        consumer = new DefaultOAuthConsumer(
                	"1sikeGafLvmGtTSvIXv78A",
                	"oVcqU2sLx39OqN9VQn2IgjOAlHwobqLZ4XCfiG8p3E",
                	SignatureMethod.HMAC_SHA1);
        provider = new DefaultOAuthProvider(consumer,
                "http://twitter.com/oauth/request_token",
                "http://twitter.com/oauth/access_token",
                "http://twitter.com/oauth/authorize");
        
	}


	public OAuthUserData retrieveOAuthUserData(String userId,String pin){								
		try {
			provider.retrieveAccessToken(pin);
			return new OAuthUserData(consumer.getToken(),consumer.getTokenSecret(),userId);
		} catch (OAuthExpectationFailedException e) {
			e.printStackTrace();
		} catch (OAuthMessageSignerException e) {
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {

			e.printStackTrace();
		}
		return null;						
	}


	public String retrieveRequestToken(String callback){			
		try {
			logger.info("callback: "+callback);
			return provider.retrieveRequestToken(callback);
		} catch (OAuthExpectationFailedException e) {

			e.printStackTrace();
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	
}
